package com.my.attendance.controller;

import com.my.attendance.entity.Department;
import com.my.attendance.entity.Employee;
import com.my.attendance.entity.Position;
import com.my.attendance.service.DepartmentService;
import com.my.attendance.service.EmployeeService;
import com.my.attendance.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/AddEmployeeServlet")
public class AddEmployeeServlet extends HttpServlet {
    private DepartmentService departmentService=new DepartmentService();
    private PositionService positionService=new PositionService();
    private EmployeeService employeeService=new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String employee_number = req.getParameter("employee_number");
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String password2 = req.getParameter("password2");
            String gender = req.getParameter("gender");
            String birthday1 = req.getParameter("birthday");
            String telephone = req.getParameter("telephone");
            String email = req.getParameter("email");
            String address = req.getParameter("address");
            String education = req.getParameter("education");
            String department_number = req.getParameter("department_number");
            String position_number = req.getParameter("position_number");
            String notes = req.getParameter("notes");
            Department department = departmentService.getDepartment(Integer.parseInt(department_number));
            Position position = positionService.getPosition(Integer.parseInt(position_number));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = sdf.parse(birthday1);
            Date in_time = new Date();
            Employee employee = new Employee(0, Integer.parseInt(employee_number), name, gender, birthday, telephone, email, address, "照片",
                    education, Integer.parseInt(department_number), Integer.parseInt(position_number),in_time, password, notes,department,position);
            System.out.println(employee);
            if (password.equals(password2)){
                int i = employeeService.addEmployee(employee);
                System.out.println(i);
                req.getRequestDispatcher("EmployeePageListServlet").forward(req,resp);
            }else {
                req.getRequestDispatcher("jsp/admin/employee_add.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
